//
//  DiscoverViewController.m
//  TenThousand
//
//  Created by baixue on 16/6/6.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "DiscoverViewController.h"
#import "FindTableCell.h"
#import "GetNewListModel.h"
#import "NewListViewController.h"
@interface DiscoverViewController ()
{
    NSMutableArray *_NewListArr;
}

@end



#define host @"www.wanyuanfu.net"
@implementation DiscoverViewController
{
    NSString *_isLastPage;
}

__strong NSString *_currentPage = @"1";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _NewListArr = [NSMutableArray array];
    
    self.title = @"发现";
    
    [self refreshPageWithMethod:@"Image.GetNewList"];
    
    [self.view addSubview:self.fingTableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadNewData:_currentPage];
}

- (void)loadNewData:(NSString *)pageindex {
//    _currentPage = pageindex;
    NSLog(@"%@", pageindex);
    NSDictionary *dict = @{
                           @"PageIndex":pageindex
                           };

    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Image.GetNewList" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        NSMutableArray *arr = object[@"Data"][0][@"listnew"];
        
        NSLog(@"%@", object);
        _isLastPage = [NSString stringWithFormat:@"%d", [object[@"Data"][0][@"PageTotal"] intValue] - 1];
        
        if ([pageindex isEqualToString:@"1"]) {
            [_NewListArr removeAllObjects];
            _currentPage = @"1";
        }
        
        for (NSDictionary *dict in arr) {
            GetNewListModel *model = [GetNewListModel mj_objectWithKeyValues:dict];
            [_NewListArr addObject:model];
            
        }
        [_fingTableView reloadData];
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
        
    } progress:^(float progress) {
        
    }];
}

- (UITableView *)fingTableView{
    
    if (!_fingTableView) {
    _fingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStylePlain];
    _fingTableView.dataSource = self;
    _fingTableView.delegate = self;
    _fingTableView.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1.00];
    _fingTableView.showsVerticalScrollIndicator = NO;
        _fingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _fingTableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (!_NewListArr) {
        return 0;
    }
    return _NewListArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!_NewListArr || _NewListArr.count == 0) {
        return nil;
    }
    FindTableCell *findCell = [tableView dequeueReusableCellWithIdentifier:@"FINDTABLECELL"];
    if (findCell == nil) {
        findCell = [[[NSBundle mainBundle] loadNibNamed:@"FindTableCell" owner:nil options:nil] lastObject];
    }
    findCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    GetNewListModel *model = _NewListArr[indexPath.row];
    [findCell setDataWithModel:model];


    return findCell;
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 280;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
    NewListViewController *newList = [[NewListViewController alloc] init];
    newList.model = _NewListArr[indexPath.row];
    [self.navigationController pushViewController:newList animated:YES];
}


//MJ刷新
-(void)refreshPageWithMethod:(NSString *)method
{
    
    //下拉刷新
    self.fingTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                      {
                                          
                                          [self loadNewData:@"1"];
                                          
                                          [self.fingTableView.mj_header endRefreshing];
                                      }];
    //上拉加载
    self.fingTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        
        
//        if ([_currentPage isEqualToString:_isLastPage]) {
//            [self.fingTableView.mj_footer endRefreshing];
//            return;
//        }
        NSInteger intger = [_currentPage integerValue];
        _currentPage = [NSString stringWithFormat:@"%ld", ++intger];
        [self loadNewData:[NSString stringWithFormat:@"%@", _currentPage]];
        
        [self.fingTableView.mj_footer endRefreshing];
    }];
    
    
}



@end
